public class Demo01 {

    // 定义一个标志位
    static boolean isQuit = false;

    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(() -> {
            while (!isQuit) {
                System.out.println("hello thread...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 线程退出
            System.out.println("线程退出...");
        });

        // 启动线程
        thread.start();
        // 休眠5秒
        Thread.sleep(5000);
        // 修改标志位，相当于李四发出了信号
        isQuit = true;
    }

    public static void main99(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
               while (!Thread.currentThread().isInterrupted()) {
                   System.out.println("hello world.");
               }
        });
        thread.start();
        System.out.println(thread.isAlive());
        Thread.sleep(100);
        thread.interrupt();  //使用interrupt可以中断线程，但是线程运行的循环条件需要配合while (!Thread.currentThread().isInterrupted())
        Thread.sleep(100);
        System.out.println(thread.isAlive());
    }
}
